<?php

namespace app\api\model;

use think\Hook;
use app\common\model\CourseOrder as CourseOrderModel;

class CourseOrder extends CourseOrderModel
{

    /**
     * 创建订单
     */
    public function add(array $data)
    {
        return $this->allowField(true)->save($data);
    }

    /**
     * 获取订单信息
     */
    public static function getByOrderNo($orderNo)
    {
        return self::where('order_no', $orderNo)->find();
    }

    /**
     * 获取用户订单
     */
    public static function getByUserId($userId, $status, $size = 20)
    {
        return self::where('user_id', $userId)->whereIn('status', $status)->order('createtime desc')->paginate($size);
    }

    /**
     * 获取用户订单列表
     */
    public static function getUserOrders($userId, $status, $size = 20)
    {
        return self::with(['teacher' => function ($query) {
            $query->withCount('course');
        }, 'refund'])->where('user_id', $userId)->whereIn('status', $status)->order('createtime desc')->paginate($size);
    }

    /**
     * 订单详情
     */
    public static function getByOrderId($courseOrderId)
    {
        return self::with(['teacher' => function ($query) {
            $query->withCount('course');
        }, 'course', 'refund'])->find($courseOrderId);
    }

    /**
     * 获取用户字段值
     */
    public static function getColumnByUserId($userId, $status, $column)
    {
        return self::where('user_id', $userId)->whereIn('status', $status)->column($column);
    }

    /**
     * 更新订单状态
     */
    public static function updateStatus($courseOrderId, $status)
    {
        return self::where('course_order_id', $courseOrderId)->update(['status' => $status]);
    }

    /**
     * 待评价
     */
    public static function waitComment($userId)
    {
        return self::where('user_id', $userId)->where('status', 3)->where('is_comment', 0)->order('createtime desc')->find();
    }

}
